if($('#auditStatus').val()==0){
	$('#audit-people-tr').hide();
}
if($('#auditStatus').val()==3){//如果审核通过 再去调用查询放款状态接口
	$('#loan-table').show();
	$('#loan-table-head').show();
	var qryLoanUrl = jQuery('#serviceUrl').val()+'/manager/qryLoanStatus?orderId='+jQuery('#orderId').val();
	jQuery.ajax({//查询放款状态
		type : 'GET',
		url : qryLoanUrl,
		dataType : 'json',
		complete : function(result) {
			if (typeof (result.responseText) == "string") {
				var data = JSON.parse(result.responseText);
			} else {
				var data = eval(result.responseText);
			}
			if (data.status == '000') {
				var reason = '';
				if(data.data.orderDetails.length > 0){//数组长度大于0说明有失败记录
					if(data.data.orderStatus=='7'){//有失败记录没成功需要手动按钮
						var button = '<td style="width: 150px;"><button id="loan-button" class="button button-primary">手动放款</button></td>';
						for(var i = 0;i<data.data.orderDetails.length;i++){
							if(i==0){
								reason += '<tr><th>自动放款状态</th><td>放款失败</td><th>失败原因</th><td>'+data.data.orderDetails[i].remark+'</td>'+button+'</tr>';

							}else{
								reason += '<tr><th>自动放款状态</th><td>放款失败</td><th>失败原因</th><td colspan="2">'+data.data.orderDetails[i].remark+'</td></tr>';
							}
						}
					}else if(data.data.orderStatus=='8'){//有失败记录，但是最后成功了
						for(var i = 0;i<data.data.orderDetails.length;i++){
							reason += '<tr><th>自动放款状态</th><td>放款失败</td><th>失败原因</th><td>'+data.data.orderDetails[i].remark+'</td></tr>';
						}
					}
					var time = data.data.loanTime ? data.data.loanTime : '';
					reason += '<tr><th>手动放款状态</th><td>'+data.data.resp+'</td><th>手动放款时间</th><td colspan="2">'+time+'</td></tr>';
					
				}else{//说明自动放款一次成功
					var time = data.data.loanTime ? data.data.loanTime : '';
					reason = '<tr><th>自动放款状态</th><td>'+data.data.resp+'</td><th>放款时间</th><td>'+time+'</td></tr>';
				}
				$('#appendTable').append(reason);
				//绑定按钮事件
				$('#loan-button').click(function(event) {
					var loanUrl = jQuery('#serviceUrl').val()+'/manager/manualLoan?orderId='+jQuery('#orderId').val()+'&applyMoney='+ jQuery('#applyMoney').val();
					BUI.Message.Confirm('确认手动放款么？', function() {
						jQuery.ajax({
							type : 'GET',
							url : loanUrl,
							dataType : 'json',
							complete : function(result) {
								if (typeof (result.responseText) == "string") {
									var data = JSON.parse(result.responseText);
								} else {
									var data = eval(result.responseText);
								}
								if (data.status == '000') {
									top.topManager.reloadPage('detail');
								} else {
									BUI.Message.Alert(data.message);
								}
							}
						});
					}, 'question');
				});
			} else {
				BUI.Message.Alert(data.message);
			}
		}
	});
}
BUI.use([ 'common/search', 'bui/overlay', 'bui/grid', 'bui/data', 'bui/form' ],
function(Search, Overlay, Grid, Data, Form) {
	$('.busOperation-btn').click(function(event) {
	var id = event.target.id;
	var operationType = 0;
	var opContent = '';
	switch (id) {
	case 'pendData-button':
		operationType = 1;
		opContent = '待补资料';
		break;
	case 'noConnect-button':
		operationType = 2;
		opContent = '未接通';
		break;
	case 'reviewPass-button':
		operationType = 3;
		opContent = '审核通过';
		break;
	case 'release-button':
		operationType = 5;
		opContent = '解约';
		break;
	default:
		alert('非法操作');
		break;
	}
	BUI.Message.Confirm('确认' + opContent + '么？', function() {
		if (operationType != 0) {
			var url = jQuery('#serviceUrl').val()+'/manager/audit?orderId='+jQuery('#orderId').val()+'&auditType='+operationType+'&remark=&operator='+jQuery('#userId').val();
			jQuery.ajax({
				type : 'GET',
				url : url,
				dataType : 'json',
				complete : function(result) {
					if (typeof (result.responseText) == "string") {
						var data = JSON.parse(result.responseText);
					} else {
						var data = eval(result.responseText);
					}
					if (data.status == '000') {
						top.topManager.reloadPage('detail');
					} else {
						BUI.Message.Alert(data.message);
					}
				}
			});
		}
	}, 'question');

})
var dialog = new Overlay.Dialog({
	title : '备注',
	width : 600,
	height : 300,
	// 配置DOM容器的编号
	contentId : 'content',
	success : function(data) {
		var url = jQuery('#serviceUrl').val()+'/manager/audit?orderId='+jQuery('#orderId').val()+'&auditType=4&remark='+jQuery('#remarkContent').val()+'&operator='+jQuery('#userId').val();
		if (jQuery('#remarkContent').val()) {
			jQuery.ajax({
				type : 'GET',
				url : url,
				dataType : 'json',
				complete : function(result) {
					if (typeof (result.responseText) == "string") {
						var data = JSON.parse(result.responseText);
					} else {
						var data = eval(result.responseText);
					}
					if (data.status == '000') {
						top.topManager.reloadPage('detail');
					} else {
						BUI.Message.Alert('备注失败！');
					}
					this.close();
				}
			});
		} else {
			BUI.Message.Alert('备注信息不许为空');
		}
	}
});

var dialog2 = new Overlay.Dialog({
	title : '解约',
	width : 400,
	height : 200,
	// 配置DOM容器的编号
	contentId : 'content2',
	success : function(data) {
        var operationType = $('#release-input').val();
		if (operationType) {
			var url = jQuery('#serviceUrl').val()+'/manager/audit?orderId='+jQuery('#orderId').val()+'&auditType=5&remark=&operator='+jQuery('#userId').val()+'&rejectCode='+operationType;
			jQuery.ajax({
				type : 'GET',
				url : url,
				dataType : 'json',
				complete : function(result) {
					if (typeof (result.responseText) == "string") {
						var data = JSON.parse(result.responseText);
					} else {
						var data = eval(result.responseText);
					}
					if (data.status == '000') {
						top.topManager.reloadPage('detail');
					} else {
						BUI.Message.Alert(data.message);
					}
				}
			});
		}else{
			BUI.Message.Alert('请选择解约原因');
		}
	this.close();
	}
});
$('#remarks-button').click(function(event) {
	jQuery('#remarkContent').val(null);
	dialog.show();
});
$('#release-btn').click(function(event) {
	dialog2.show();
});
});

$('.contractA').click(function (){
	var vbsBid = $('#vbsBid').val();
	if(vbsBid==null ||''==vbsBid){
		BUI.Message.Alert('vbs业务号不存在');
		return ;
	}
	var filetype = '';
	var value = $(this)[0].id;
	switch (value) {
	case 'contract1':
		filetype = 'DOCUMENTKIND/XINYONGHUA/JIEKUANBUCHONGHETONG';//借款补充合同
		break;

	case 'contract2':
		filetype = 'DOCUMENTKIND/XINYONGHUA/XINYONGBUCHONGHETONG';//信用补充合同
		break;

	case 'contract3':
		filetype = 'DOCUMENTKIND/XINYONGHUA/GRXXSQS';//个人信息授权书
		break;

	case 'contract4':
		filetype = 'DOCUMENTKIND/XINYONGHUA/JIEKUANHETONG';//借款合同
		break;

	case 'contract5':
		filetype = 'DOCUMENTKIND/XINYONGHUA/KOUKUANSHOUQUANSHU';//扣款授权书
		break;

	case 'contract6':
		filetype = 'DOCUMENTKIND/XINYONGHUA/XINYONGHETONG';//信用合同
		break;

	case 'contract7':
		filetype = 'DOCUMENTKIND/XINYONGHUA/TEBIETISHIHAN';//特别提示函
		break;

	default:
		break;
	}
	var url='business/contract?filetype='+filetype+'&&Bid='+vbsBid;
	top.topManager.openPage({
	    id : 'contract',
	    href : url,
	    title : '合同'
	 })
});